/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Windows;
using System.Data.Objects;
using System.Linq;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    class TransferTableRightComb : BindInterface
    {
        public TransferTableRightComb(string name = "")
        {
            Text = name;
        }


        override public System.Windows.Visibility IsVisible
        {
            get
            {
                try
                {
                    if (CombineTableBindings.SelectedTable != null &&
                        CombineTableBindings.SelectedLeftUser != null &&
                        CombineTableBindings.SelectedRightUser != null &&
                        CombineTableBindings.SelectedTable2 != null &&
                        CombineTableBindings.SelectedTable.Id != CombineTableBindings.SelectedTable2.Id)
                    {
                        return Visibility.Visible;
                    }
                    return Visibility.Hidden;
                }
                catch (Exception e) { Logging.LogException(e); return System.Windows.Visibility.Hidden; }
            }
        }

        override public void Clicked()
        {
            try
            {

                int checksAtRightTable = CombineTableBindings.SelectedTable2.CheckCount;
                int checksAtLeftTable = CombineTableBindings.SelectedTable.CheckCount;

                ///Do not allow merging of tables that have accepted payment already.  It will cause too much
                ///headache in the reporting and will open the doors for a ton of errors only for a small 
                ///if nothing feature gain.
                if (!CombineTableBindings.SelectedTable2.TRANSACTIONS.Where(t=> !t.WasVoided).Any() &&
                    !CombineTableBindings.SelectedTable.TRANSACTIONS.Where(t => !t.WasVoided).Any() && checksAtRightTable == 1
                    && checksAtLeftTable == 1 && CombineTableBindings.SelectedTable.GIFT_CARDS_SOLDS.Count() == 0)
                {

                    ///First start out by transfering all sales from the left table to the right.  However, do
                    ///keep in mind that we need to update the items seat index.
                    int seatsAtRightTable = CombineTableBindings.SelectedTable2.GuestCount;
                    int seatsAtLeftTable = CombineTableBindings.SelectedTable.GuestCount;

                    ///Increase the guest count on the right table.
                    CombineTableBindings.SelectedTable2.GuestCount += seatsAtLeftTable;

                    ///Move the sales over.
                    foreach (SALE s in CombineTableBindings.SelectedTable.SALES.ToList())
                    {
                        s.OwningSeatNumber = s.OwningSeatNumber + seatsAtRightTable;
                        s.ORDER = CombineTableBindings.SelectedTable2;

                        s.UserId = CombineTableBindings.SelectedTable2.UserId;

                        CombineTableBindings.SelectedTable.SALES.Remove(s);
                        CombineTableBindings.SelectedTable2.SALES.Add(s);
                    }

                    if (CombineTableBindings.SelectedTable.TRANSACTIONS.Where(t => !t.WasVoided).Any())
                    {
                        foreach (TRANSACTION t in CombineTableBindings.SelectedTable.TRANSACTIONS)
                        {
                            t.OwningCheckNumber = 1;
                            t.ORDER = CombineTableBindings.SelectedTable2;
                            t.NavOrderId = CombineTableBindings.SelectedTable2.Id;
                            CombineTableBindings.SelectedTable2.TRANSACTIONS.Add(t);
                        }
                    }

                    CombineTableBindings.SelectedTable.NOSALES.Clear();
                    CombineTableBindings.SelectedTable.SHIFT = null;
                    SHIFT.GlobalContext.ORDERS.DeleteObject(CombineTableBindings.SelectedTable);
                    SHIFT.GlobalContext.SaveChanges();
                    Oceanside.ControlPanelWindow.Show();
                    UMD.ShowGuiMessageSTASafe_NOTHROW("Success!");
                }
                else
                {
                    UMD.ShowGuiMessageSTASafe_NOTHROW("You cannot merge tables that have already accepted a form of payment.");
                }
            }
            catch (Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_PLACEHOLDER, exc);
            }
        }
    }
}
